package com.fc.mall.service.impl;

import com.fc.mall.dao.OmsOrderGroupBuyDao;
import com.fc.mall.dto.OmsOrderGroupBuyInfo;
import com.fc.mall.dto.OmsOrderGroupBuyQueryParam;
import com.fc.mall.mapper.OmsOrderGroupBuyMapper;
import com.fc.mall.service.OmsOrderGroupBuyService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.net.URLDecoder;
import java.util.List;

@Service
public class OmsOrderGroupBuyServiceImpl implements OmsOrderGroupBuyService {
    @Autowired
    private OmsOrderGroupBuyMapper orderGroupBuyMapper;

    @Autowired
    private OmsOrderGroupBuyDao omsOrderGroupBuyDao;

    @Override
    public List<OmsOrderGroupBuyInfo> queryOrderGroupBuys(Long productGroupId, OmsOrderGroupBuyQueryParam param,
                                                          Integer pageSize, Integer pageNum) {
        PageHelper.startPage(pageNum, pageSize, "create_time");
        List<OmsOrderGroupBuyInfo> omsOrderGroupBuys = omsOrderGroupBuyDao.queryOrderGroupBuys(productGroupId, param);
        omsOrderGroupBuys.stream().forEach(omsOrderGroupBuy ->{
           omsOrderGroupBuy.setNickname(transferNickname(omsOrderGroupBuy.getNickname()));
           omsOrderGroupBuy.getGroupBuyItemList().stream().forEach(item ->
                   item.setNickname(transferNickname(item.getNickname()))
           );
        }

        );

        return omsOrderGroupBuys;
    }

    private String transferNickname(String nickname){
        try{
           return URLDecoder.decode(StringUtils.isEmpty(nickname) ? "" : nickname, "utf-8");
        } catch (Exception e){

        }
        return "";
    }
}
